/****
	Copyright 2012 Taras Kulbachka

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package com.atdevsoft.apps.abyss;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.IntentService;
import android.content.Intent;

public class FavsNotifierService extends IntentService {
	public static final int QUOTE_SOURCE_ABYSSBEST = 1;
	
	private static final String NOTIFICATION_ADDRESS = "http://abyssquotes.appspot.com/api/putQuote";    

	// incoming 
    public static final String ACTION_SEND_QUOTE = "com.atdevsoft.apps.abyss.SEND_QUOTE";
    public static final String INTENT_EXTRA_QUOTE_SOURCE = "quote_source";
    public static final String INTENT_EXTRA_QUOTE_SOURCE_ID = "quote_id";
    public static final String INTENT_EXTRA_QUOTE_EXTRA_TITLE = "quote_extra";
    public static final String INTENT_EXTRA_QUOTE = "quote";
    public static final String INTENT_EXTRA_SENDER = "sender";

    public FavsNotifierService(String name) {
        super(name);
    }

    public FavsNotifierService() {
        super("FavsNotifierService");
    }
    
    private void postQuote(int quoteSource, String id, String extra, String quote, String sender) {
   	    HttpClient client = new DefaultHttpClient();
   	    HttpPost query = new HttpPost(NOTIFICATION_ADDRESS);
   	    try {
   	    	ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(4);
   	    	params.add(new BasicNameValuePair("source", String.valueOf(quoteSource)));
   	    	params.add(new BasicNameValuePair("sourceId", id));
   	    	params.add(new BasicNameValuePair("sourceExtraTitle", extra));
   	    	params.add(new BasicNameValuePair("quote", quote));
   	    	if (sender != null)
   	    		params.add(new BasicNameValuePair("sender", sender));
   	        
   	    	query.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

   	    	client.execute(query);

   	    } catch (ClientProtocolException e) {
   	    } catch (IOException e) {
   	    }
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if (intent.getAction().equals(ACTION_SEND_QUOTE)) {
        	final int quoteSource = intent.getExtras().getInt(INTENT_EXTRA_QUOTE_SOURCE, QUOTE_SOURCE_ABYSSBEST);
        	final String quoteId = intent.getExtras().getString(INTENT_EXTRA_QUOTE_SOURCE_ID);
        	final String quoteExtra = intent.getExtras().getString(INTENT_EXTRA_QUOTE_EXTRA_TITLE);
        	final String quote = intent.getExtras().getString(INTENT_EXTRA_QUOTE);
        	final String sender = intent.getExtras().getString(INTENT_EXTRA_SENDER);
        	
        	if ((quote != null) && (!quote.trim().equals(""))) {
        		postQuote(quoteSource, quoteId, quoteExtra, quote, sender);
        	}  
        }
    }
}
